<!-- Datatables -->
<link href="cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" />
<link href="/vendors/datatables.net-bs/css/dataTables.bootstrap.min.css" rel="stylesheet" />
<!-- page content -->
<div id="modelDiv" class="right_col" role="main">
    <div class="">
        <div class="title_left">
            <h3>Roles <small>RoleList</small></h3>
        </div>
    </div>

    <div class="clearfix"></div>

    <div class="row">
        <div class="x_panel">
            <div class="x_title">
                <button type="reset" class="btn btn-success" data-toggle="modal" data-target="#addByModel"
                    @click="clearAddModal()">添加</button>
                <ul class="nav navbar-right panel_toolbox">
                    <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                            aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                            <a class="dropdown-item" href="#">Settings 1</a>
                            <a class="dropdown-item" href="#">Settings 2</a>
                        </div>
                    </li>
                    <li><a class="close-link"><i class="fa fa-close"></i></a>
                    </li>
                </ul>
                <div class="clearfix"></div>
            </div>
            <div class="x_content">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="card-box table-responsive">
                            <table id="modelTable" class="table table-striped table-bordered" style="width:100%">
                                <thead>
                                    <tr>
                                        <th>Id</th>
                                        <th>RoleName</th>
                                        <th>createDate</th>
                                        <th>operate</th>
                                    </tr>
                                </thead>
                                <tbody>

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--- 引入新增模态框 -->
    <div th:replace="account/roleAdd"></div>
    <div th:replace="account/roleUpdate"></div>
</div>
</div>
<!-- Datatables -->
<script src="/vendors/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="/vendors/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>

<script type="text/javascript">
    var modelVue = new Vue({
        el: "#modelDiv",
        data: {

            //默认页长
            defultPageSize: 5,

            role: {
                id: 0,
                roleName: "",
                createDate: "",
            },

            searchBean: {
                //当前页
                currentPage: 1,
                pageSize: 5,
                orderBy: "id",
                drection: "asc",
                //关键字查询
                keyWord: "",
            },

            //页面表格的数据结构
            tableData: {
                draw: 0,
                recordsTotal: 0,
                recordsFiltered: 0,
                //这里是每行的数据
                data: [],
            },
        },
        methods: {
            //定义行数据结构
            rowData: function (id, roleName, createDate) {
                var row = {};
                row.id = id;
                row.roleName = roleName;
                row.createDate = createDate;
                row.operate = function () {
                    return (
                        "<a href='#' class='btn_editcolor' data-toggle='modal' data-target='#updateByModel' " +
                        "onclick='updateModel(\"" +
                        id +
                        "\")'>编辑</a>&nbsp;" +
                        "<a href='javascript:void(0);' onclick='deleteRole(\"" +
                        id +
                        "\")' class='btn_editcolor'>删除</a>"
                    );
                };
                return row;
            },
            //进入页面加载表格数据
            loadTable: function (pageSize) {
                var self = this;
                $("#modelTable").DataTable({
                    paging: true, //分页
                    serverSide: true, //开启后端分页
                    pagingType: "full_numbers", //分页样式的类型simple/simple_numbers/full/full_numbers
                    pageLength: pageSize, //定义初始的页长
                    processing: true,
                    destroy: true, //允许销毁替换，在表格重新查询时，可以自动销毁以前的data
                    lengthChange: true, //控制是否能够调整每页的条数
                    lengthMenu: [5, 10, 25, 50], // 控制页长
                    searching: true,
                    "data-show-refresh": true,
                    ordering: true,
                    autoWidth: true,//自动隐藏
                    ajax: function (data, callback, settings) {
                        // 从data获取查询数据
                        var columIndex = data.order[0].column;
                        var direction = data.order[0].dir;
                        var orderBy = data.columns[columIndex].name;
                        pageSize = data.length == undefined ? pageSize : data.length;

                        self.searchBean.currentPage = data.start / pageSize + 1;
                        self.searchBean.pageSize = pageSize;
                        self.searchBean.orderBy = orderBy;
                        self.searchBean.direction = direction;
                        self.searchBean.keyWord = data.search.value;

                        $.ajax({
                            url: "/api/roles",
                            type: "post",
                            contentType: "application/json",
                            data: JSON.stringify(self.searchBean),
                            success: function (rs) {
                                if (!rs) {
                                    layer.alert("请求出错，请稍后重试" + rs.errmsg, { icon: 2 });
                                    callback(self.tableData);
                                    return;
                                }
                                if (rs.list == null) {
                                    $("#moduleTable tbody tr").remove();
                                    $("#loading").remove();
                                    callback(self.tableData);
                                    return;
                                }
                                $("#loading").remove();
                                var rowsData = [];
                                $.each(rs.list, function (i, item) {
                                    //调用上方定义好的行数据结构进行赋值
                                    var rowData = self.rowData(
                                        item.id,
                                        item.roleName,
                                        item.createDate
                                    );
                                    // 将行数据放到数组里
                                    rowsData.push(rowData);
                                });
                                self.tableData.data = rowsData;
                                self.tableData.recordsTotal = rs.total;
                                self.tableData.recordsFiltered = rs.total;
                                callback(self.tableData);
                            },
                            error: function (data) {
                                layer.alert(data.responseText, { icon: 0 });
                            },
                        });
                    },
                    columns: [
                        //定义行数据字段
                        { data: "id", name: "id", sortable: true },
                        { data: "roleName", name: "roleName", sortable: true },
                        { data: "createDate", name: "create_date", sortable: true },
                        { data: "operate", width: "80px", sortable: false },//操作列
                    ],
                });
            },
            clearAddModal: function () {
                this.role = {
                    id: 0,
                    roleName: "",
                    createDate: "",
                };
            },
            addRole: function () {
                var self = this;
                $.ajax({
                    url: "/api/role",
                    type: "post",
                    contentType: "application/json",
                    data: JSON.stringify(self.role),
                    success: function (rs) {
                        if (rs.status == 200) {
                            //添加成功时关闭模态框并且刷新数据
                            self.loadTable(this.defultPageSize);
                        } else {
                            layer.alert(rs.message, { icon: 0 });
                        }
                    },
                    error: function (data) {
                        layer.alert(data.responseText, { icon: 0 });
                    },
                });
            },
            //初始化模态框
            updateModel: function (id) {
                var self = this;
                $.ajax({
                    url: "/api/role/" + id,
                    type: "get",
                    success: function (rs) {
                        self.role = rs;
                    },
                    error: function (data) {
                        layer.alert(data.responseText, { icon: 0 });
                    },
                });
            },
            updateRole: function () {
                var self = this;
                $.ajax({
                    url: "/api/role",
                    type: "put",
                    contentType: "application/json",
                    data: JSON.stringify(self.role),
                    success: function (rs) {
                        if (rs.status == 200) {
                            //修改成功时关闭模态框并且刷新数据
                            self.loadTable(this.defultPageSize);
                        } else {
                            layer.alert(rs.message, { icon: 0 });
                        }
                    },
                    error: function (data) {
                        layer.alert(data.responseText, { icon: 0 });
                    },
                });
            },
            deleteRole: function (id) {
                var self = this;
                //弹出窗口让用户进行确认
                bootbox.confirm("你确定要删除吗？", function (result) {
                    if (result) {
                        $.ajax({
                            url: "/api/role/" + id,
                            type: "delete",
                            success: function (rs) {
                                if (rs.status == 200) {
                                    //删除成功时刷新数据
                                    self.loadTable(this.defultPageSize);
                                } else {
                                    layer.alert(rs.message, { icon: 0 });
                                }
                            },
                            error: function (data) {
                                layer.alert(data.responseText, { icon: 0 });
                            },
                        });
                    }
                });
            },
        },
        created: function () {
            //因为定义的修改模态框是vue下的要把他的生命周期修改为在window下
            window.updateModel = this.updateModel;
            window.deleteRole = this.deleteRole;
        },
        mounted: function () {
            this.loadTable(this.defultPageSize);
        },
    });
</script>